package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-11-08
 * Time: 19:30
 */
public class UserDao {
    public User serachByuserId(int userId) throws SQLException {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        DButi dButi = new DButi();
        connection = dButi.getconnect();
        String sql = "select * from user where userId = ?";
        statement = connection.prepareStatement(sql);
        statement.setInt(1, userId);
        resultSet = statement.executeQuery();
        if(resultSet.next()){
            User user = new User();
            user.setUserId(userId);
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));
            dButi.close(connection, statement, resultSet);
            return user;
        }
        dButi.close(connection,statement,resultSet);
        return null;
    }

    public User serachByusername(String username) throws SQLException {
        DButi dButi = new DButi();
        Connection connection = dButi.getconnect();
        String sql = "select * from user where username = ?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setString(1, username);
        ResultSet resultSet = statement.executeQuery();
        if(resultSet.next()){
            User user = new User();
            user.setUserId(user.getUserId());
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));
            user.setUserId(resultSet.getInt("userId"));
            dButi.close(connection, statement, resultSet);
            return user;
        }
        dButi.close(connection,statement,resultSet);
        return null;
    }
}
